﻿using JustSiteWorker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace JustSiteWorker
{
    public partial class JustSiteContainer : DbContext
    {
        public JustSiteContainer(string connString)
            : base(connString)
        { }
    }
}

namespace JustSiteWorkrole.DAL
{
    public class Repository : IDisposable
    {
        private JustSiteContainer db = new JustSiteContainer(
           Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.IsAvailable
               ? Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue("SQL.Connection") //CloudConfigurationManager.GetSetting("SQL.Connection") 
               : "name=JustSiteContainer");

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    db.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Save()
        {
            db.SaveChanges();
        }

        #region Users
        public List<User> Users
        {
            get { return db.Users.ToList(); }
        }

        public User GetUser(User user)
        {
            if (user == null) return null;
            return db.Users.FirstOrDefault(a => a.Password == user.Password);
        }
        public User GetUser(int id)
        {
            return db.Users.Find(id);
        }
        public User GetUser(string name, string password)
        {
            return db.Users.FirstOrDefault(a => a.Name.ToLower() == name.ToLower() && a.Password == password);
        }
        public User GetUser(string cod)
        {
            if (string.IsNullOrEmpty(cod)) return null;
            return db.Users.FirstOrDefault(a => a.UrlId.ToLower() == cod.ToLower());
        }

        public void CreateUser(User user)
        {
            db.Users.Add(user);
            Save();
        }

        public void EditUser(User user)
        {
            if (db.Entry(user).State == EntityState.Detached)
            {
                db.Users.Attach(user);
            }
            db.Entry(user).State = EntityState.Modified;
            Save();
        }

        public void UpdateSession(int id)
        {
            UpdateSession(GetUser(id));
        }
        public void UpdateSession(User user)
        {
            user.LastVisit = DateTime.Now;
            EditUser(user);
        }
        #endregion

        #region Comments
        public List<Comment> Comments
        {
            get { return db.Comments.ToList(); }
        }

        public Comment GetComment(int id)
        {
            return db.Comments.Find(id);
        }

        public void AddComment(Comment comment)
        {
            db.Comments.Add(comment);
            Save();
        }

        public Comment GetLastComment()
        {
            return Comments.LastOrDefault();
        }
        #endregion

        #region Statistiks
        public List<Statistika> Statistika
        {
            get { return db.StatistikaSet.ToList(); }
        }

        public void AddStatistika(Statistika statistika)
        {
            db.StatistikaSet.Add(statistika);
            Save();
        }

        public List<Statistika> GetStatistika(User user)
        {
            if (user == null) return null;
            return db.StatistikaSet.Where(a => a.UserId == user.Id).ToList();
        }

        public List<Statistika> GetStatistika(int userId)
        {
            return GetStatistika(GetUser(userId));
        }
        #endregion
    }
}